package o2o.web.actions.base;

import o2o.config.dao.UserDaoSql;
import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.EncrypAES;
import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.sys.SysCustomerCache;
import o2o.cache.user.UserInfoMemory;
import o2o.dao.PO.sys.SysCustomerInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.HashMap;

// 管理员 账号锁定/解锁

@Controller
public class UserLockAction {

    //  查询
    @RequestMapping(method = RequestMethod.POST, value = "/base/user-lock/select")
    @ResponseBody
    public String select(String apiKey, String sysAppKey) {
        try {
            String decrypt = EncrypAES.aesDecrypt(apiKey, "abcdefgabcdeg12p");
            if (ValidateUtil.isNull(sysAppKey)) {
                sysAppKey = SysCustomerCache.getInstance().getDefaultKey();
            }

            SysCustomerInfo customerInfo = SysCustomerCache.getInstance().getInfo(sysAppKey);
            if (null != customerInfo) {
                if (!decrypt.equals(customerInfo.getApiKey())) {
                    return AjaxResult.errorResult("您没有访问权限");
                }
            } else {
                return AjaxResult.errorResult("没有客户信息");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.errorResult(e.getMessage());
        }

        String lock = this.select_utf8mb4();
        String lock1 = this.select_utf8();
        String isLock = lock == null ? lock1 : lock;
        return AjaxResult.successResult(isLock);
    }

    //  修改
    @RequestMapping(method = RequestMethod.POST, value = "/base/user-lock/update")
    @ResponseBody
    public String modify(String apiKey, String sysAppKey, String lock) {
        try {
            String decrypt = EncrypAES.aesDecrypt(apiKey, "abcdefgabcdeg12p");
            if (ValidateUtil.isNull(sysAppKey)) {
                sysAppKey = SysCustomerCache.getInstance().getDefaultKey();
            }

            SysCustomerInfo customerInfo = SysCustomerCache.getInstance().getInfo(sysAppKey);
            if (null != customerInfo) {
                if (!decrypt.equals(customerInfo.getApiKey())) {
                    return AjaxResult.errorResult("您没有访问权限");
                }
            } else {
                return AjaxResult.errorResult("没有客户信息");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.errorResult(e.getMessage());
        }

        this.update_utf8mb4(lock);
        this.update_utf8(lock);
        //修改缓存
        UserInfoMemory.getInstance().run();//重新初始化用户信息
        return AjaxResult.successResult();
    }

    private void update_utf8(String lock) {
        StringBuilder sb = new StringBuilder();
        sb.append("UPDATE user_info SET isLock =  ").append(lock)
                .append(" WHERE username COLLATE utf8_unicode_ci IN (SELECT  t.username FROM ")
                .append(" (SELECT cu2.username FROM  sys_city_company_user cu2  WHERE cu2.companyId IN ")
                .append("(SELECT cu.companyId  FROM  sys_city_company_user cu LEFT JOIN user_info u ON  cu.username=u.username COLLATE utf8_unicode_ci WHERE u.userType = 99 ")
                .append(")) t) ;");
        try {
            UserDaoSql.getInstance().executeBySql(sb.toString());
        } catch (SQLException ex) {
//            ex.printStackTrace();
        }
    }

    private void update_utf8mb4(String lock) {
        StringBuilder sb = new StringBuilder();
        sb.append("UPDATE user_info SET isLock =  ").append(lock)
                .append(" WHERE username COLLATE utf8mb4_unicode_ci IN (SELECT  t.username FROM ")
                .append(" (SELECT cu2.username FROM  sys_city_company_user cu2  WHERE cu2.companyId IN ")
                .append("(SELECT cu.companyId  FROM  sys_city_company_user cu LEFT JOIN user_info u ON  cu.username=u.username COLLATE utf8mb4_unicode_ci WHERE u.userType = 99 ")
                .append(")) t) ;");
        try {
            UserDaoSql.getInstance().executeBySql(sb.toString());
        } catch (SQLException ex) {
//            ex.printStackTrace();
        }
    }


    private String select_utf8() {
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT DISTINCT islock  FROM user_info ")
                .append(" WHERE username COLLATE utf8_unicode_ci IN (SELECT  t.username FROM ")
                .append(" (SELECT cu2.username FROM  sys_city_company_user cu2  WHERE cu2.companyId IN ")
                .append("(SELECT cu.companyId  FROM  sys_city_company_user cu LEFT JOIN user_info u ON  cu.username=u.username COLLATE utf8_unicode_ci WHERE u.userType = 99 ")
                .append(")) t) ;");
        HashMap<String, Object> map = new HashMap<>();
        try {
            map = UserDaoSql.getInstance().queryMapBySql(sb.toString());
        } catch (SQLException ex) {
//            ex.printStackTrace();
            map = null;
        }
        if (map != null) {
            return map.get("islock").toString();
        } else {
            return null;
        }
    }

    private String select_utf8mb4() {
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT DISTINCT islock  FROM user_info ")
                .append(" WHERE username COLLATE utf8mb4_unicode_ci IN (SELECT  t.username FROM ")
                .append(" (SELECT cu2.username FROM  sys_city_company_user cu2  WHERE cu2.companyId IN ")
                .append("(SELECT cu.companyId  FROM  sys_city_company_user cu LEFT JOIN user_info u ON  cu.username=u.username COLLATE utf8mb4_unicode_ci WHERE u.userType = 99 ")
                .append(")) t) ;");
        HashMap<String, Object> map = new HashMap<>();
        try {
            map = UserDaoSql.getInstance().queryMapBySql(sb.toString());
        } catch (SQLException ex) {
//            ex.printStackTrace();
            map = null;
        }
        if (map != null) {
            return map.get("islock").toString();
        } else {
            return null;
        }
    }
}
